/*
 * Copyright 2003-2007 (c) Live Media Pty Ltd. <argot@einet.com.au> 
 *
 * This software is licensed under the Argot Public License 
 * which may be found in the file LICENSE distributed 
 * with this software.
 *
 * More information about this license can be found at
 * http://www.einet.com.au/License
 * 
 * The Developer of this software is Live Media Pty Ltd,
 * PO Box 4591, Melbourne 3001, Australia.  The license is subject 
 * to the law of Victoria, Australia, and subject to exclusive 
 * jurisdiction of the Victorian courts.
 */

package com.colony.remote.rpc;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

import com.argot.remote.MetaInterface;
import com.argot.remote.MetaLocation;
import com.argot.remote.MetaMethod;
import com.colony.remote.RemoteException;
import com.colony.remote.RemoteRuntimeException;

public class RpcMetaStub
implements InvocationHandler
{
	MetaLocation _location;
	MetaInterface _mClass;
	RpcMetaClient _client;
	
	public RpcMetaStub( RpcMetaClient client, MetaLocation location , MetaInterface mClass )
	{
		_client = client;
		_location = location;
		_mClass = mClass;
	}

	public Object invoke(Object proxy, Method method, Object[] args)
	throws Throwable
	{
		// First look for a client side stub that will override
		// the default request mechanism.
/*
		Method stubMethod = null;
		try
		{
			// The method we're looking for must have the same
			// parameters as the original method.
			if ( args != null )
			{
				Class[] cargs = method.getParameterTypes();
					
				stubMethod = this.getClass().getMethod( method.getName(), cargs );
			}	
			else
			{
				stubMethod = this.getClass().getMethod( method.getName(), null );
			}
		}
		catch (SecurityException e)
		{
		}
		catch (NoSuchMethodException e)
		{
		}
		
		// We have an overriding method handler.
		if ( stubMethod != null )
		{
			try
			{
				return stubMethod.invoke( this, args );
			}
			catch (IllegalArgumentException e1)
			{
				throw e1;
			}
			catch (IllegalAccessException e1)
			{
				throw e1;
			}
			catch (InvocationTargetException e1)
			{
				throw e1.getTargetException();
			}
		}	
*/
		// Find the MetaMethod information.
		MetaMethod mMethod = _mClass.getMetaMethod( method );
		if ( mMethod == null )
		{
			throw new RemoteException("method not available");
		}
		
		// Create and process the request.
		RpcMetaMethodRequest request = new RpcMetaMethodRequest( _location,  mMethod, args );
		
		_client.process( request );
		
		// Check for errors and return values.
		if ( request.getException() != null )
		{
			// if the exception returned is declared, throw it.
			Throwable throwable = request.getException();
			Class exceptionTypes[] = method.getExceptionTypes();
			for (int x=0;x<exceptionTypes.length;x++)
			{
				if (exceptionTypes[x].isAssignableFrom(throwable.getClass()))
				{
						throw throwable;
				}
			}

			// if the exception isn't declared, throw a remoteRuntimeException.
			throw new RemoteRuntimeException("Exception not declared",throwable);
		}
		Class retType = method.getReturnType();
		if ( retType == Void.class || retType == void.class )
			return null;
		
		return request.getReturnValue();
	}

	public String type()
	{
		return _mClass.getTypeName();
	}


}
